package com.example.testrecorder.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author Yingyong Lao
 * 创建时间 2021/6/10 22:44
 * @version 1.0
 */
public class BitmapUtil {
    /**
     * 从所给的路径获取位图
     * @return 位图
     */
    public static Bitmap getBitmapFromPath(String imgPath){
        Bitmap bitmap=null;
        BufferedInputStream bufferedInputStream=null;
        try {
            bufferedInputStream=new BufferedInputStream(new FileInputStream(imgPath));
            bitmap = BitmapFactory.decodeStream(bufferedInputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if(bufferedInputStream!=null){
                try {
                    bufferedInputStream.close();//关闭缓冲输入流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bitmap;
    }

    /**
     * 旋转图片
     * @param angle
     * @param bitmap
     * @return
     */
    public static Bitmap rotaingImageView(int angle , Bitmap bitmap) {
        //旋转图片 动作
        Matrix matrix = new Matrix();;
        matrix.postRotate(angle);
        // 创建新的图片
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }
}
